package 多线程;

/**
 * @Author: 李佳芬
 * @Description: 龟兔赛跑
 * @DateTime: 2023/3/27 16:21
 **/
public class Track implements Runnable{

    String name;

    @Override
    public void run() {
        for (int i = 0;i<=100;i++){
            boolean b = victory(i);
            if (b){
                break;
            }
            if (i%10==0||Thread.currentThread().getName().equals("兔子")){
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
        }
    }

    private boolean victory(int i){

        if(name!=null){
            return true;
        }
        if (i>=100){
            name = Thread.currentThread().getName();
            System.out.println("胜利者是"+name);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Track track = new Track();
        new Thread(track,"兔子").start();
        new Thread(track,"乌龟").start();
    }
}
